import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import React from 'react'
export interface IConfirmDialog {
  visible?: boolean
  title: string
  onOk?: (() => void) | undefined | null
  onCancel?: (() => void) | undefined | null
  okText?: string
  cancelText?: string
  content: any
}


interface AppStoreState {
  themeColor: string
  confirmDialog: IConfirmDialog
  menuVisible: boolean
  userInfo: any //个人信息包括企业信息
}

const appDefaultState: AppStoreState = {
  themeColor: '#FFAE36',
  confirmDialog: {
    visible: false,
    title: '标题',
    onOk: null,
    onCancel: null,
    okText: '确认',
    cancelText: '取消',
    content: ''
  },
  menuVisible: true,
  userInfo: null
}

const appSlice = createSlice({
  name: 'app',
  initialState: appDefaultState,
  reducers: {
    handleTheme: (state: AppStoreState, action: PayloadAction<string>) => {
      state.themeColor = action.payload
    },
    handlingDialogs: (state: AppStoreState, action: PayloadAction<IConfirmDialog>) => {
      state.confirmDialog = {
        ...state.confirmDialog,
        ...action.payload
      }
    },
    handleChangeMenuVisible: (state: AppStoreState, action: PayloadAction<boolean>) => {
      state.menuVisible = action.payload
    },
    handleUpdateUserInfo: (state: AppStoreState, action: PayloadAction<number[]>) => {
      state.userInfo = action.payload
    }
  }
})

export const { handleTheme, handlingDialogs, handleChangeMenuVisible, handleUpdateUserInfo } = appSlice.actions

export default appSlice.reducer
